import { BaseContext } from 'koa';

export default (service: any) => ({
    async create(ctx: BaseContext, next: any) {
        // 用户登录校验
        if (ctx.user === 'no') {
            ctx.body = { code: 1, message: '未登录用户无此权限' };
            return;
        }
        if (ctx.user === 'expired') {
            ctx.body = { code: 1, message: '用户信息已过期，请重新登录' };
            return;
        }
        // 参数校验
        const _param = ctx.request.body;
        if (!_param.commodity_name) {
            ctx.body = { code: 1, message: '商品名称不能为空' };
            return;
        }
        if (!_param.commodity_des) {
            ctx.body = { code: 1, message: '商品描述不能为空' };
            return;
        }
        if (!_param.commodity_type) {
            ctx.body = { code: 1, message: '商品种类不能为空' };
            return;
        }
        _param.user_id = ctx.user.id;

        const commodity = await service.commodity.create(_param).catch((err: any) => {
            ctx.body = { code: 1, message: err.message };
            return;
        });
        if (!commodity) {
            ctx.body = { code: 1, message: '创建失败' };
            return;
        }
        ctx.rest({
            id: commodity._id
        });
    },
    async update(ctx: BaseContext, next: any) {
        // 权限校验
        if (ctx.user === 'no') {
            ctx.body = { code: 1, message: '未登录用户无此权限' };
            return;
        }
        if (ctx.user === 'expired') {
            ctx.body = { code: 1, message: '用户信息已过期，请重新登录' };
            return;
        }
        // 参数校验
        const _param = ctx.request.body;
        if (!_param.id) {
            ctx.body = { code: 1, message: 'id不能为空' };
            return;
        }
        _param.user_id = ctx.user.id;

        // 请求数据库
        const commodity = await service.commodity.update(_param).catch((err: any) => {
            ctx.body = { code: 1, message: err.message };
            return;
        });
        if (!commodity) {
            ctx.body = { code: 1, message: '更新失败' };
            return;
        }
        ctx.rest();
    },
    async remove(ctx: BaseContext, next: any) {
        // 权限校验
        if (ctx.user === 'no') {
            ctx.body = { code: 1, message: '未登录用户无此权限' };
            return;
        }
        if (ctx.user === 'expired') {
            ctx.body = { code: 1, message: '用户信息已过期，请重新登录' };
            return;
        }
        // 参数校验
        const _param = ctx.request.body;
        if (!_param.id) {
            ctx.body = { code: 1, message: 'id不能为空' };
            return;
        }
        const _commodity = service.commodity.findById(_param.id);
        if (_commodity.user_id !== ctx.user.id) {
            ctx.body = { code: 1, message: '无删除权限' };
            return;
        }
        const commodity = await service.commodity.remove(_param.id).catch((err: any) => {
            ctx.body = { code: 1, message: err.message };
            return;
        });
        if (!commodity) {
            ctx.body = { code: 1, message: '删除失败' };
            return;
        }
        ctx.rest();
    },
    async findById(ctx: BaseContext, next: any) {
        const param = ctx.request.body;
        if (!param.id) {
            ctx.body = { code: 1, message: 'id不能为空' };
            return;
        }
        const commodity = await service.commodity.findById(param.id).catch((err: any) => {
            ctx.body = { code: 1, message: err.message };
            return;
        });
        if (!commodity) {
            ctx.body = { code: 0, message: '查询失败' };
            return;
        }
        ctx.rest({
            id: commodity._id,
            memo: commodity.memo,
            status: commodity.status,
            create_at: commodity.create_at,
            update_at: commodity.update_at
        });
    },
    async find(ctx: BaseContext, next: any) {
        const param = ctx.request.body;
        param.page_size = param.page_size || 10;
        param.page_no = param.page_no > 0 ? param.page_no : 1;
        const commodity = await service.commodity.find(param).catch((err: any) => {
            ctx.body = { code: 1, message: err.message };
            return;
        });
        ctx.rest(commodity);
    },
    async findByUser(ctx: BaseContext, next: any) {
        // 权限校验
        if (ctx.user === 'no') {
            ctx.body = { code: 1, message: '未登录用户无此权限' };
            return;
        }
        if (ctx.user === 'expired') {
            ctx.body = { code: 1, message: '用户信息已过期，请重新登录' };
            return;
        }
        const _param = ctx.request.body;
        _param.user_id = ctx.user.id;
        const commodity = await service.commodity.findByUser(_param).catch((err: any) => {
            ctx.body = { code: 1, message: err.message };
            return;
        });
        ctx.rest(commodity);
    }
});